<template>
  <!-- 横向柱状图 -->
  <div class="com-container">
    <div class="com-chart" ref="seller"></div>
  </div>
</template>

<script>
import { mapState } from "vuex";
export default {
  data() {
    return {
      chartInstance: null,
      allData: null,
      totalPage: 0, // 一共多少页
      page: 1, // 展示第几页

      timeId: null,
    };
  },
  computed: {
    ...mapState(["theme"]),
  },
  watch: {
    theme() {
      console.log("主题切换了");
      // 先销毁实例
      this.chartInstance.dispose();
      this.initChart();
      this.updateChart()
      this.screenAdapter()
      // this.startInterval();
    },
  },
  created() {
    // 在组件创建完成后，进行回调函数注册
    this.$socket.registerCallBack("sellerData", this.getData);
  },
  mounted() {
    // 初始化Charts
    this.initChart();
    // 获取数据
    // this.getData();
    // 告诉服务器，我现在需要数据
    this.$socket.send({
      action: "getData",
      socketType: "sellerData",
      chartName: "seller",
      value: "",
    });
    // 适配
    this.screenAdapter();
    window.addEventListener("resize", this.screenAdapter);
  },
  destroyed() {
    clearInterval(this.timeId);
    this.$socket.unRegisterCallBack("sellerData");
    window.removeEventListener("resize", this.screenAdapter);
  },
  methods: {
    async getData(res) {
      // const { data: res } = await this.$http.get("seller");
      this.allData = res;
      console.log(res);
      this.allData.sort((a, b) => {
        return a.value - b.value;
      });

      this.totalPage =
        this.allData.length % 5 === 0
          ? this.allData.length / 5
          : this.allData.length / 5 + 1;

      this.updateChart();
      this.startInterval();
    },
    initChart() {
      this.chartInstance = this.$echarts.init(this.$refs.seller, this.theme);
      // 对图标初始化的控制
      const initOption = {
        title: {
          text: "▎商家销售统计",
          left: 20,
          top: 20,
        },
        grid: {
          // 坐标轴
          top: "20%",
          left: "3%",
          right: "6%",
          bottom: "3%",
          containLabel: true, // 距离是包含坐标轴的文字的
        },
        xAxis: {
          type: "value",
        },
        yAxis: {
          type: "category",
        },
        tooltip: {
          // 当鼠标移入axis(坐标轴)时展示 底层的背景色
          trigger: "axis",
          backgroundColor: "#222733",
          borderColor: "#666666",
          textStyle: {
            color: "#fff",
          },
          axisPointer: {
            // 展示的类型是线条类型
            type: "line",
            lineStyle: {
              cap: "round",
              type: "solid",

              color: "#2D3443",
            },
            // 相等于 z-index 将层级调低
            z: 0,
          },
        },
        series: [
          {
            type: "bar",

            label: {
              // 文字展示
              show: true,
              position: "right",
              textStyle: {
                color: "white",
              },
            },
            itemStyle: {
              // 柱样式设置
              barBorderRadius: [0, 33, 33, 0], // 柱状圆角属性
              // 指明颜色渐变的方向
              // 指明不同百分比之下颜色的值
              color: new this.$echarts.graphic.LinearGradient(0, 0, 1, 0, [
                // 百分之0下的颜色值
                {
                  offset: 0,
                  color: "#5052EE",
                },
                // 百分之100下的颜色值
                {
                  offset: 1,
                  color: "#AB6EE5",
                },
              ]),
            },
          },
        ],
      };
      this.chartInstance.setOption(initOption);

      //  鼠标时间监听
      this.chartInstance.on("mouseover", () => {
        clearInterval(this.timeId);
      });
      this.chartInstance.on("mouseout", () => {
        this.startInterval();
      });
    },
    updateChart() {
      let start = (this.page - 1) * 5;
      let end = this.page * 5;
      let showData = this.allData.slice(start, end);
      let sellerName = showData.map((item) => {
        return item.name;
      });
      let sellerValue = showData.map((item) => {
        return item.value;
      });

      let dataOption = {
        yAxis: {
          data: sellerName,
        },
        series: [
          {
            data: sellerValue,
          },
        ],
      };
      this.chartInstance.setOption(dataOption);
    },
    startInterval() {
      if (this.timeId) {
        clearInterval(this.timeId);
      }
      this.timeId = setInterval(() => {
        this.page++;
        if (this.page > this.totalPage) {
          this.page = 1;
        }
        this.updateChart();
      }, 3000);
    },
    // 浏览器大小发生变化会调用此方法
    screenAdapter() {
      let titleFontSize = (this.$refs.seller.offsetWidth / 100) * 3.6;
      let adapterOption = {
        title: {
          textStyle: {
            fontSize: titleFontSize,
          },
        },

        tooltip: {
          axisPointer: {
            lineStyle: {
              width: titleFontSize,
            },
          },
        },
        series: [
          {
            barWidth: titleFontSize, // 柱状宽度
            itemStyle: {
              // 柱样式设置
              barBorderRadius: [0, titleFontSize / 2, titleFontSize / 2, 0], // 柱状圆角属性
            },
          },
        ],
      };
      this.chartInstance.setOption(adapterOption);
      this.chartInstance.resize();
    },
  },
};
</script>

<style></style>
